import { getBandwidthTypeListApi } from '@/apis/modules/bandwidthType.ts'
import { ipStatusOptions } from '@/enums/ip.ts'

interface HookOptions {
  regions: Ref<LabelValue[]>
}

export default function useConfig(options: HookOptions) {
  const { parse } = useLangField()

  const bandwidthOptions = ref<LabelValue[]>([])

  const tableColumns = buildColumns([
    {
      title: 'IP地址',
      dataIndex: 'ip',
      format: 'copy',
    },
    {
      title: '带宽类型',
      dataIndex: 'bandwidth_type',
      render({ record }) {
        return getOptionLabel(record.bandwidth_type, bandwidthOptions.value)
      },
    },
    {
      title: '所属区域',
      dataIndex: 'region_id',
      render({ record }) {
        return getOptionLabel(record.region_id, options.regions.value)
      },
    },
    {
      title: '分配状态',
      dataIndex: 'status',
      render({ record }) {
        return getOptionLabel(record.status, ipStatusOptions)
      },
    },
    {
      title: '创建时间',
      dataIndex: 'created_at',
      format: 'date-time',
      sortable: sortableSettings,
    },
    {
      title: '更新时间',
      dataIndex: 'updated_at',
      format: 'date-time',
      sortable: sortableSettings,
    },
    {
      title: '操作',
      cellClass: 'actions-cell',
      width: 90,
      slotName: 'actions',
    },
  ])

  async function getBandwidthData() {
    const res = await getBandwidthTypeListApi({})
    bandwidthOptions.value = (res.list || []).map((item) => {
      const name = parse(item.name || '')
      return { label: name?.['zh-CN'] || '-', value: item.id! }
    })
  }

  onMounted(getBandwidthData)

  return {
    tableColumns,
  }
}
